﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ServerAdder.Core.Model;

namespace ServerAdder.Common
{
    public class Common
    {
        private static Common common;

        public static Common Instant
        {
            get
            {
                if (common == null)
                    common = new Common();

                return common;
            }
            set
            {
                common = value;
            }
        }

        public void WriteSetting(string server, string user)
        {
            try
            {
                List<string> texts = new List<string>();
                texts.Add(string.Format("server: {0}", server));
                texts.Add(string.Format("username: {0}", user));

                File.WriteAllLines(AppCostant.FullSettingPathFile, texts);
            }
            catch 
            {
            }
        }

        public LocalUser GetUserInformation()
        {
            try
            {
                var texts = File.ReadAllLines(AppCostant.FullSettingPathFile).ToList();
                string server = texts[0].Substring(7, texts[0].Length - 7).Trim();
                string username = texts[1].Substring(9, texts[1].Length - 9).Trim();

                var user = new LocalUser(server, username);
                return user;
            }
            catch
            {
                return new LocalUser(string.Empty, string.Empty);
            }
        }
    }
}
